import { PLAYMODE, LYRICSHOW } from '../config.js'
//音乐播放
const state = {
	album: null,//专辑
	catalogId: '',//音频id
	currentTime: 0,//播放时间
	duration: 0,//播放时长
	lyricIndex: 0,//歌词索引
	lyricTitle: '',//当前歌词
	currentPage: 1,//请求页数
	isLastPage: false,//请求完成
	nextUrl: '',//请求下一页
	catalogs: [],//章节列表
	randoms: [],//随机播放列表
	characters: [],//相关用户
	recomes: [],//相关用户
	lyrics: [],//歌词列表
	parentRecomes: [],//合集推荐
	parentCharacters: [],//合集角色
	paused: true,//是否暂停
	playMode: uni.getStorageSync(PLAYMODE) || 'round', //播放模式
	lyricShow: uni.getStorageSync(LYRICSHOW) || false, //是否显示全局歌词
}

const getters = {
	getAlbum (state) {
		return state.album
	},
	getCatalogId (state) {
		return state.catalogId
	},
	getCurrentTime (state) {
		return state.currentTime
	},
	getDuration (state) {
		return state.duration
	},
	getLyricIndex (state) {
		return state.lyricIndex
	},
	getLyricTitle (state) {
		return state.lyricTitle
	},
	getCurrentPage (state) {
		return state.currentPage
	},
	getIsLastPage (state) {
		return state.isLastPage
	},
	getNextUrl (state) {
		return state.nextUrl
	},
	getCatalogs (state) {
		return state.catalogs
	},
	getRandoms (state) {
		return state.randoms
	},
	getCharacters (state) {
		return state.characters
	},
	getRecomes (state) {
		return state.recomes
	},
	getParentCharacters (state) {
		return state.parentCharacters
	},
	getParentRecomes (state) {
		return state.parentRecomes
	},
	getLyrics (state) {
		return state.lyrics
	},
	getPaused (state) {
		return state.paused
	},
	getPlayMode (state) {
		return state.playMode
	},
	getLyricShow (state) {
		return state.lyricShow
	}
}

const mutations = {
	setAlbum (state, album) {
		state.album = album
	},
	setCatalogId (state, catalogId) {
		state.catalogId = catalogId
	},
	setCurrentTime (state, currentTime) {
		state.currentTime = currentTime
	},
	setDuration (state, duration) {
		state.duration = duration
	},
	setLyricIndex (state, lyricIndex) {
		state.lyricIndex = lyricIndex
	},
	setLyricTitle (state, lyricTitle) {
		state.lyricTitle = lyricTitle
	},
	setCurrentPage (state, currentPage) {
		state.currentPage = currentPage
	},
	setIsLastPage (state, isLastPage) {
		state.isLastPage = isLastPage
	},
	setNextUrl (state, nextUrl) {
		state.nextUrl = nextUrl
	},
	//设置播放列表
	setCatalogs (state, catalogs) {
		state.catalogs = catalogs
	},
	//设置随机播放列表
	setRandoms (state, randoms) {
		state.randoms = randoms
	},
	//设置用户列表
	setCharacters (state, characters) {
		state.characters = characters
	},
	//设置推荐列表
	setRecomes (state, recomes) {
		state.recomes = recomes
	},
	//设置合集角色
	setParentCharacters (state, characters) {
		state.parentCharacters = characters
	},
	//设置合集推荐
	setParentRecomes (state, recomes) {
		state.parentRecomes = recomes
	},
	//设置歌词列表
	setLyrics (state, lyrics) {
		state.lyrics = lyrics
	},
	//设置当前播放状态
	setPaused (state, paused) {
		state.paused = paused
	},
	//设置播放模式
	setPlayMode (state, playMode) {
		state.playMode = playMode || 'round'
		uni.setStorageSync(PLAYMODE, state.playMode)
	},
	//设置是否显示全局歌词
	setLyricShow (state, bol) {
		state.lyricShow = bol
		uni.setStorageSync(LYRICSHOW, state.lyricShow)
	}
}

const actions = {
	//保存歌曲
	save ({state, commit}, song) {
		let list = [...state.catalogs]
		const index = list.findIndex(item => item.id == song.id)
		index > -1 ? list[index] = song : list.push(song)
		commit('setCatalogs', list)
	},
	//移除指定歌曲
	remove ({state, commit}, id) {
		const list = [...state.catalogs]
		const index = list.findIndex(item => item.id == id)
		if ( index > -1 )list.splice(index, 1)
		commit('setCatalogs', list)
	},
	//切换播放模式
	switchPlayMode ({state, commit}) {
		switch ( state.playMode ) {
			case 'round':
				commit('setPlayMode', 'loop')
				break
			case 'loop':
				commit('setPlayMode', 'random')
				break
			default:
				commit('setPlayMode', 'round')
				break
		}
	},
	//切换歌词显示
	switchLyricShow ({state, commit}) {
		commit('setLyricShow', !state.lyricShow)
	},
	prev ({state, commit}) {
		const list = state.playMode == 'random' ? state.randoms : state.catalogs
		if ( state.playMode != 'loop' && list.length > 1 ) {
			let catalogId = state.catalogId
			while (true) {
				const index = list.findIndex(item => item.id == catalogId)
				let catalog
				if ( index > -1 ) { catalog = list[index - 1 < 0 ? list.length - 1 : index - 1] }
				else {  catalog = list.length > 0 ? list[0] : null }
				if ( !catalog ) break
				catalogId = catalog.id
				if ( !catalog.isVip && (catalog.contextUrl || catalog.detailUrl || catalog.src) ) {
					commit('setCurrentTime', 0)
					commit('setDuration', 1)
					commit('setPaused', true)
					commit('setCatalogId', catalogId)
					break
				}
			}
		} else { uni.$emit('seekSong', 0) }
	},
	next ({state, commit, dispatch}) {
		const list = state.playMode == 'random' ? state.randoms : state.catalogs
		if ( state.playMode != 'loop' && list.length > 1 ) {
			let catalogId = state.catalogId
			while (true) {
				const index = list.findIndex(item => item.id == catalogId)
				let catalog
				if ( index > -1 ) { catalog = list[index + 1 > list.length - 1 ? 0 : index + 1] }
				else { catalog = list.length > 0 ? list[0] : null }
				if ( !catalog ) break
				catalogId = catalog.id
				if ( !catalog.isVip && (catalog.contextUrl || catalog.detailUrl || catalog.src) ) {
					commit('setCurrentTime', 0)
					commit('setDuration', 1)
					commit('setPaused', true)
					commit('setCatalogId', catalogId)
					break
				}
			}
		} else { uni.$emit('seekSong', 0) }
	},
	change ({state, commit}, id) {
		const list = state.playMode == 'random' ? state.randoms : state.catalogs
		const index = list.findIndex(item => item.id == id)
		if ( index > -1 ) {
			commit('setCurrentTime', 0)
			commit('setDuration', 1)
			commit('setPaused', true)
			commit('setCatalogId', id)
		}
	},
	//停止音频播放
	stop ({commit}) {
		commit('setPaused', true)
		commit('setCatalogId', '')
		commit('setCurrentTime', 0)
		commit('setDuration', 1)
		commit('setLyricIndex', 0)
		commit('setLyricTitle', '')
	},
	//销毁音频
	destroy ({state, commit}) {
		commit('setAlbum', null)
		commit('setCatalogId', '')
		commit('setCurrentTime', 0)
		commit('setDuration', 1)
		commit('setLyricIndex', 0)
		commit('setLyricTitle', '')
		commit('setCurrentPage', 1)
		commit('setIsLastPage', false)
		commit('setNextUrl', '')
		commit('setCatalogs', [])
		commit('setRandoms', [])
		commit('setRecomes', [])
		commit('setCharacters', [])
		commit('setParentRecomes', [])
		commit('setParentCharacters', [])
		commit('setLyrics', [])
		commit('setPaused', true)
	}
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
	actions
}
